<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/8/008
 * Time: 18:23
 */

namespace app\common\service;


use app\api\controller\Base;
use think\Db;

class App extends Base
{

    /**
     * 生成uuid
     */
    public static function getUuid($user_id,$rand = 1 )
    {
        $uuid = 1010000+$user_id*1000+rand(100,976)+$rand;

        $has  = Db::name('users')->where('uuid',$uuid)->value('user_id');

        if ($has){
            $rand++;
            return self::getUuid($uuid,$rand);
        }
        return $uuid;

    }

    public static function _limit($time = 1,$id = 0,$msg = '请求过于频繁,请稍后重试')
    {
        $key   = request()->path().$id;
        $cache = cache('apiLimit'.$key);
        if ($cache){
            api_return(0,$msg);
        }else{
            cache('apiLimit'.$key,1,$time);
        }
    }

    /**
     * @param int $id
     * 销毁限流参数
     */
    public static function _destroyLimit($id = 0){
        $key   = request()->path().$id;
        cache('apiLimit'.$key,null);

    }



}